package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private static final String FALG_REG = "reg";//注册页面
    private static final String FALG_REG_SUBMIT = "rs";//提交注册
    private static final String FALG_LOGIN_SUBMIT = "ls";//提交登录
    private static final String FALG_INFO = "info";//用户信息页面
    private static final String FALG_LOGOUT = "logout";//注销
    private static final String FALG_LOGIN = "login";//登录页面

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");
        if (StringUtils.isBlank(flag)) {//跳转到登录页面
            toLoginPage(request, response);
        } else if (FALG_REG.equals(flag)) {//跳转到注册页面 reg
            toRegisterPage(request, response);
        } else if (FALG_REG_SUBMIT.equals(flag)) {//执行注册动作
            register(request, response);
        } else if (FALG_INFO.equals(flag)) {//跳转到用户
            toUserInfo(request, response);
        } else if (FALG_LOGOUT.equals(flag)) {//用户注销
            logout(request, response);
        } else if (FALG_LOGIN.equals(flag)) {//用户跳转到登录页面
            toLoginPage(request, response);
        } else if (FALG_LOGIN_SUBMIT.equals(flag)) {//执行登录
            login(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    //执行动作
    private  void register(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        String username = request.getParameter("username");
        String mobilePhone = request.getParameter("phone");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session=request.getSession();
        String checkcode=(String)session.getAttribute("checkcode");
        System.out.print(checkcode);
        if(checkcode.equalsIgnoreCase(vicode))
        {
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setMobilePhone(mobilePhone);
            String userId=userService.saveUser(user);
            System.out.println("=====" + userId);
            User user2=userService.findUserById(userId);
            session.setAttribute("user",user2);
            request.getRequestDispatcher("WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            return;
        }
        else
        {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("WEB-INF/jsp/member/register.jsp").forward(request,response);
        }
    }

    //跳转到注册页面
    private void toRegisterPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        request.getRequestDispatcher("WEB-INF/jsp/member/register.jsp").forward(request,response);
    }


    private void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user=userService.findUserByNamePw(username,password);
        if(user!=null)
        {
            HttpSession session=request.getSession();
            session.setAttribute("user",user);
            request.getRequestDispatcher("index.jsp").forward(request,response);
            return;
        }
        else
        {
            request.setAttribute("message","用户名或密码不正确不正确");
            request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request,response);
        }
    }

    private void logout(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        HttpSession session=request.getSession();
        session.invalidate();
        request.getRequestDispatcher("/ProductServlet").forward(request,response);
    }
    private void toUserInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        request.getRequestDispatcher("WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
    }
    private void toLoginPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request,response);
    }

}
